package com.zj.inoutput.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 数据返回类
 * 遵循"成功给结果,失败说原因"的理念
 *
 * @author DuanYu <a>Email:z-houjie@qq.com</a>
 * @date 2022-01-21 20:31
 */
@Data
public class ResultData implements Serializable {

    private static final long serialVersionUID = 3727595832232514350L;

    /**
     * 返回码
     */
    private Integer code;

    /**
     * 结果描述
     */
    private String msg;

    /**
     * 实体信息，具体的数据传输对象
     */
    private Object data;

    public static final Integer SUCCESS_CODE = 666;

    public static final String SUCCESS_MSG = "成功";

    public static final Integer FAIL_CODE = 444;

    public static final String FAIL_MSG = "处理失败";

    public static ResultData successful(Object data) {
        ResultData resultData = new ResultData();
        resultData.setCode(SUCCESS_CODE);
        resultData.setMsg(SUCCESS_MSG);
        resultData.setData(data);
        return resultData;
    }

    public static ResultData successful() {
        return successful(null);
    }

    public static ResultData failed(Object data, String msg) {
        ResultData resultData = new ResultData();
        resultData.setCode(FAIL_CODE);
        resultData.setMsg(msg);
        resultData.setData(data);
        return resultData;
    }

    public static ResultData failed(String msg) {
        return failed(null, msg);
    }

}
